/***************************************************************************
 *   Copyright (C) 2008 by Tomasz Dubik                                    *
 *   tdubik@gmail.com                                                      *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include <QFile>
#include <QDebug>
#include <QtGlobal>

#include "taskcontainer.h"
#include "task.h"
#include "globals.h"


TaskContainer::TaskContainer(QObject* parent):QObject(parent)
{
	//not implemented
}

TaskContainer::~TaskContainer()
{
	while(!m_tasks.isEmpty())
		delete m_tasks.takeFirst();
}

void TaskContainer::addTask(const QString& taskTxt, const QDate& date, const TaskType& type, const TaskPriority& priority, const int percent)
{
	Task* task = new Task(taskTxt, date, type, priority, percent);
	m_tasks.append(task);
}

void TaskContainer::addTask(Task* task)
{
	Q_ASSERT(task);
	m_tasks.append( new Task(*task));
}

void TaskContainer::saveData()
{
	QFile file(DATAFILE);

	if( file.open(QIODevice::WriteOnly | QIODevice::Truncate))
	{
		QTextStream stream(&file);

		foreach( Task* task, m_tasks)
			task->externalize(stream);
	}
}

void TaskContainer::loadData(const QString& fileName)
{
	QFile file(fileName);
	if( file.exists())
	{
		if (file.open(QIODevice::ReadOnly))
		{
			QTextStream stream(&file);
			QString line;
			QString tmp;
			bool ok = false;
			Task* task;
			int i = 0, j = 0;
			line = stream.readAll();
			while ((j = line.indexOf(EndOfTask,i)) != -1)
			{
				task = new Task();
				//task->internalize(line);
				tmp = line.mid(i,j-i);
				task->internalize(tmp);
				if( -1 == findTask( *task ))
				{
					m_tasks.append(task);
					//emit(taskAdded(task->taskTxt(), task->date(), task->type(), task->priority(), task->percent()));
					emit(taskAdded(*task));
				}
				else
				{
					delete task;
				}
				i = j + EndOfTaskLength;
			}
		}
	}
}

void TaskContainer::editTask(int itemInd, Task& task )
{	
	Task* lTask = m_tasks.at( itemInd );
	lTask->copy( task );
}

void TaskContainer::editTask(int itemInd, const QString& taskTxt, const QDate& date,
							 const TaskType& type, const TaskPriority& priority, const int percent)
{
	Task* task = m_tasks.at(itemInd);
	task->setTaskTxt( taskTxt );
	task->setDate( date );
	task->setType( type );
	task->setPriority( priority );
	task->setPercent( percent );
}

int TaskContainer::findTask(const QString& taskTxt, const QDate& date, const TaskType& type, const TaskPriority& priority)
{
	Task task(taskTxt, date, type, priority, 0);
	int ind = findTask(task);
	return ind;
}

int TaskContainer::findTask(Task& task)
{
	Task* ltask;
	for(int i = 0; i < m_tasks.count(); i++)
	{
		ltask = m_tasks.at(i);
		if( task.date() == ltask->date() && task.priority() == ltask->priority() && task.taskTxt() == ltask->taskTxt() && task.type() == ltask->type())
		{
			return i;
		}
	}
	//if not found
	return -1;
}

void TaskContainer::deleteTask(Task& task)
{
	int ind = findTask(task);
	if( ind != -1 )
	{
		Task* task = m_tasks.at(ind);
		m_tasks.removeAt(ind);
		delete task;
	}
	else
		qDebug() << "unable to find task";
}
//EOF
